/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.appspot.trick.it.admin.api;

import com.appspot.trick.it.admin.Util;
import com.appspot.trick.it.admin.db.SiteMstEntity;
import com.appspot.trick.it.admin.model.ParameterMstModel;
import com.google.api.client.auth.oauth2.Credential;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLEncoder;

/**
 *
 * @author takahara
 */
public class GaeParameterMstAPIService {
    private final SiteMstEntity siteMstEntity;
    private final GoogleOauthAPIService googleOauthAPIService;
    
    private static final String GET_URI = "_ah/api/parameterMstEndpoint/v1/get";
    private static final String PUT_URI = "_ah/api/parameterMstEndpoint/v1/put";
    
    /**
     * コンストラクタ 
     * @param siteMstEntity
     * @throws java.io.IOException 
     */
    public GaeParameterMstAPIService(SiteMstEntity siteMstEntity) throws IOException {
        this.siteMstEntity = siteMstEntity;
        this.googleOauthAPIService = new GoogleOauthAPIService();
    }
    
    /**
     * 記事の変更
     * @param model
     * @return
     * @throws Exception 
     */
    public ParameterMstModel put(ParameterMstModel model) throws Exception {

        // アクセストークンの更新
        Credential credential = googleOauthAPIService.getCredentials();

        Type responseModelType = new TypeToken<ParameterMstModel>() {
        }.getType();

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("title=");
        psb.append(URLEncoder.encode(model.getTitle(), "utf-8"));
        psb.append("&");
        psb.append("description=");
        if (model.getDescription() != null) {
            psb.append(URLEncoder.encode(model.getDescription(), "utf-8"));
        }
        psb.append("&");
        psb.append("keyWord=");
        if (model.getKeyWordList() != null && model.getKeyWordList().size() > 0 && Util.keywordListToString(model.getKeyWordList()) != null) {

            psb.append(URLEncoder.encode(Util.keywordListToString(model.getKeyWordList()), "utf-8"));
        }
        psb.append("&");
        psb.append("topContent=");
        if (model.getTopContent() != null) {
            psb.append(URLEncoder.encode(model.getTopContent(), "utf-8"));
        }

        return (ParameterMstModel) Http.postGetResponseJson(
                new URL(siteMstEntity.getDomin() + PUT_URI),
                psb.toString(),
                responseModelType,
                credential.getAccessToken());
    }
    
    /**
     * 取得
     * @return 
     * @throws java.lang.Exception 
     */
    public ParameterMstModel get() throws Exception {
        
        // アクセストークンの更新
        Credential credential = googleOauthAPIService.getCredentials();
        
        String respons = Http.get(
                siteMstEntity.getDomin() + GET_URI, credential.getAccessToken());

        Type responseModelType = new TypeToken<ParameterMstModel>() {}.getType();

        Gson gson = new Gson();
        
        ParameterMstModel model = gson.fromJson(respons, responseModelType);
        
        return model;
    }
    
}
